package cn.ismartv.launchertest;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huaijie on 9/15/15.
 */
public class XmlInfoHandler extends DefaultHandler {
    private final int HOMEPAGE = 1;
    private final int TIMESTAMP = 2;
    private final int POSTER = 3;
    private final int POSITION = 4;
    private final int TITLE = 5;
    private final int IMAGE = 6;
    private final int ACTION = 7;
    private final int HOMEPAGE_TEMPLATE = 8;
    private final int HOMEPAGE_URL = 9;
    private final int FROMPAGE = 10;
    private final int URL = 11;


    private int flg = 0;

    private XmlEntity xmlEntity;
    private List<XmlEntity.PosterEntity> posterEntityList;


    private XmlEntity.PosterEntity posterEntity;

    public XmlEntity getXmlEntity() {
        return xmlEntity;
    }

    @Override
    public void startDocument() throws SAXException {
        xmlEntity = new XmlEntity();
        posterEntityList = new ArrayList<>();
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }


    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (localName.equals("homepage")) {
            flg = HOMEPAGE;
            return;
        }

        if (localName.equals("timestamp")) {
            flg = TIMESTAMP;
            return;
        }


        if (localName.equals("poster")) {
            posterEntity = new XmlEntity.PosterEntity();
            flg = POSTER;
            return;
        }

        if (localName.equals("position")) {
            flg = POSITION;
            return;
        }

        if (localName.equals("title")) {
            flg = TITLE;
            return;
        }


        if (localName.equals("image")) {
            flg = IMAGE;
            return;
        }

        if (localName.equals("action")) {
            flg = ACTION;
            return;
        }

        if (localName.equals("homepage_template")) {
            flg = HOMEPAGE_TEMPLATE;
            return;
        }

        if (localName.equals("homepage_url")) {
            flg = HOMEPAGE_URL;
            return;
        }

        if (localName.equals("fromPage")) {
            flg = FROMPAGE;
            return;
        }
        if (localName.equals("url")) {
            flg = URL;
            return;
        }


    }


    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (localName.equals("poster")) {
            posterEntityList.add(posterEntity);
            posterEntity = null;
            return;
        }

        if (localName.equals("homepage")) {
            xmlEntity.setPosterEntityList(posterEntityList);
            posterEntityList = null;
            return;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String str = new String(ch, start, length);

        switch (flg) {
            case TIMESTAMP:
                xmlEntity.setTimestamp(str);
                break;
            case POSITION:
                posterEntity.setPosition(str);
                break;
            case TITLE:
                posterEntity.setTitle(str);
                break;
            case IMAGE:
                posterEntity.setImage(str);
                break;
            case ACTION:
                posterEntity.setAction(str);
                break;
            case HOMEPAGE_TEMPLATE:
                posterEntity.setHomepage_template(str);
                break;
            case HOMEPAGE_URL:
                posterEntity.setHomepage_url(str);
                break;
            case FROMPAGE:
                posterEntity.setFromPage(str);
                break;
            case URL:
                posterEntity.setUrl(str);
                break;
            default:
                return;
        }

    }
}
